<?php

namespace System\Models;

class Curuser extends \System\Core\Model
{

    /**
     * True if the user is logged in
     * @var boolean
     */
    private $_loggedIn = FALSE;
    
    /**
     * Object with the current users table row.
     * @var object
     */
    private $_data = NULL;
    
    function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Manual login where the user name and password is sent in plain text.
     * @param string $name
     * @param string $password
     * @return boolean true if the user is successfully logged in. 
     */
    public function logIn($name, $password)
    {
        $this->db->where('name', $name);
        $this->db->get('users');
        $this->_data = $this->db->row();

        // Does the pasword match?
        if ($this->db->num_rows() == 1 && comparePassword($password, $this->_data->salt, $this->_data->password))
        {
            // Then set the cookies
            $this->_loggedIn = TRUE;
            $this->input->setCookie('uid', $this->_data->id);
            $this->input->setCookie('password', $this->_data->password);
            $this->db->update('users', array('last_login' => time()), array('id', $this->_data->id));
            return TRUE;
        }
        
        return FALSE;
    }
    
    /**
     * Auto logins the user if the user has the correct cookie info set.
     * @return boolean true if the user is logged in. 
     */
    public function cookieLogIn()
    {
        if ($this->input->getCookie('uid') != FALSE && $this->input->getCookie('password') != FALSE )
        {
            $this->db->where('id', $this->input->getCookie('uid'));
            $this->db->where('password', $this->input->getCookie('password'));
            $this->db->get('users');  

            // Does the password match the cookie?
            if ($this->db->num_rows() == 1)
            {
                $this->_loggedIn = TRUE;
                $this->_data = $this->db->row();
                $this->db->update('users', array('last_access' => time()), array('id', $this->input->getCookie('uid')));
                return TRUE;
            }
        }
        
        return FALSE;
    }
    
    /**
     * Logs out the user by cleaning the cookies.
     */
    public function logOut()
    {
        $this->input->deleteCookie('uid');
        $this->input->deleteCookie('password');
    }
    
    /**
     * Used to login the user or redirect him to somewhere else if it fails.
     * Use this method to stop a user from accessing a specific page unless
     * he's logged in.
     * 
     * @param string $redirect optional redirect to this non default page.
     */
    public function loginOrRedirect($redirect = NULL)
    {
        if (!$this->cookieLogIn())
        {
            $redirect = !is_null($redirect) ? $redirect : 'login';
            $this->fc->siteRedirect($redirect);
        }
    }
    
    /**
     * Checks if the user is logged in.
     * @return boolean TRUE if logged in. 
     */
    public function isLoggedIn()
    {
        return $this->_loggedIn;
    }
}
?>
